package com.rmdjzz.slmmn.controller.user.rankingList;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.rmdjzz.slmmn.pojo.base.Song;
import com.rmdjzz.slmmn.pojo.result.Result;
import com.rmdjzz.slmmn.pojo.vo.PageVo;
import com.rmdjzz.slmmn.pojo.vo.SongVo;
import com.rmdjzz.slmmn.service.SingerService;
import com.rmdjzz.slmmn.service.SongService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.ArrayList;
import java.util.List;

/**
 * @作者 张绍鑫
 * @创建时间 2023/6/5
 */
@Controller
@RequestMapping("/user")
@Api(tags = "排行榜api文档")
@Slf4j
public class RankingListController {
    @Autowired
    private SongService songService;
    @Autowired
    private SingerService singerService;

    /**
     * 需求：
     *      将所有歌曲的歌名和歌手根据热度排行
     *      top1-对所有进行分页排序查询
     *      top2-分页数据进行vo转换
     *      top3-返回结果集
     * @param pageVo
     * @return
     */
    @GetMapping("/hot")
    @ResponseBody
    @ApiOperation("热歌榜")
    public Result hot(PageVo pageVo){

        Page<Song> page = new Page<>(pageVo.getStartIndex(), pageVo.getSize());
        //根据热度排行
        QueryWrapper<Song> songQueryWrapper = new QueryWrapper<>();
        songQueryWrapper.orderByDesc("num");
        //查询到数据
        Page songPage = songService.page(page,songQueryWrapper);
        //将数据转为vo
        List<Song> records = songPage.getRecords();
        ArrayList<SongVo> songVos = new ArrayList<>();
        for (Song song:records){
            SongVo songVo = new SongVo();
            songVo.setSongName(song.getSongName());
            if (singerService.getById(song.getSingerId())==null){
                songVo.setSingerName("未知歌手");
            }else {
                songVo.setSingerName(singerService.getById(song.getSingerId()).getSingerName());
            }
            songVos.add(songVo);
        }
        songPage.setRecords(songVos);
        return Result.ok(songPage);
    }

    @GetMapping("/soaring")
    @ResponseBody
    @ApiOperation("飙升榜")
    public Result soaring(PageVo pageVo){

        Page<Song> page = new Page<>(pageVo.getStartIndex(), pageVo.getSize());
        //根据热度排行
        QueryWrapper<Song> songQueryWrapper = new QueryWrapper<>();
        songQueryWrapper.orderByDesc("heat");
        //查询到数据
        Page songPage = songService.page(page,songQueryWrapper);
        //将数据转为vo
        List<Song> records = songPage.getRecords();
        ArrayList<SongVo> songVos = new ArrayList<>();
        for (Song song:records){
            SongVo songVo = new SongVo();
            songVo.setSongName(song.getSongName());
            if(singerService.getById(song.getSingerId())==null){
                songVo.setSingerName("未知歌手");
            }else {
                songVo.setSingerName(singerService.getById(song.getSingerId()).getSingerName());
            }
            songVos.add(songVo);
        }
        songPage.setRecords(songVos);
        return Result.ok(songPage);
    }
    @GetMapping("/playRankList/{type}")
    @ApiOperation("榜单播放")
    public String playRankList(@PathVariable String type, Model model){
        if (type.equals("hot")){
            //根据播放次数排行
            QueryWrapper<Song> songQueryWrapper = new QueryWrapper<>();
            songQueryWrapper.orderByDesc("num");
            //查询到数据
            List<Song> records = songService.list(songQueryWrapper);
            //将数据转为vo

            ArrayList<SongVo> songVos = new ArrayList<>();
            ArrayList<String> words = new ArrayList<>();
            for (Song song:records){
                SongVo songVo = new SongVo();
                songVo.setId(song.getId());
                songVo.setSongImg(song.getSongImg());

                songVo.setWords(song.getWords());
                songVo.setSongName(song.getSongName());
                songVo.setSongTime(song.getSongTime());
                if (singerService.getById(song.getSingerId())==null){
                    songVo.setSingerName("未知歌手");
                }else {
                    songVo.setSingerName(singerService.getById(song.getSingerId()).getSingerName());
                }
                songVo.setSong(song.getSong());
                log.info("song："+song);
                log.info("songVo："+songVo);
                words.add(song.getWords());
                songVos.add(songVo);
            }
            model.addAttribute("list",songVos);
            model.addAttribute("words",words);
            model.addAttribute("msg","热歌榜");
            return "play";
        }else if (type.equals("soaring")){
            //根据播放次数排行
            QueryWrapper<Song> songQueryWrapper = new QueryWrapper<>();
            songQueryWrapper.orderByDesc("heat");
            //查询到数据
            List<Song> records = songService.list(songQueryWrapper);
            //将数据转为vo

            ArrayList<SongVo> songVos = new ArrayList<>();
            ArrayList<String> words = new ArrayList<>();
            for (Song song:records){
                SongVo songVo = new SongVo();
                songVo.setId(song.getId());
                songVo.setSongImg(song.getSongImg());

                songVo.setWords(song.getWords());
                songVo.setSongName(song.getSongName());
                songVo.setSongTime(song.getSongTime());
                if (singerService.getById(song.getSingerId())==null){
                    songVo.setSingerName("未知歌手");
                }else {
                    songVo.setSingerName(singerService.getById(song.getSingerId()).getSingerName());
                }
                songVo.setSong(song.getSong());
                log.info("song："+song);
                log.info("songVo："+songVo);
                words.add(song.getWords());
                songVos.add(songVo);
            }
            model.addAttribute("list",songVos);
            model.addAttribute("words",words);
            model.addAttribute("msg","飙升榜");
            return "play";
        }
        return null;
    }
}
